package api

import (
	"blizzard/app/model"
	"blizzard/app/service"
	"blizzard/app/utils/code"
	"blizzard/app/utils/response"
	"github.com/gogf/gf/net/ghttp"
)

var SignCard = new(apiSignCard)

type apiSignCard struct {}

func (a *apiSignCard) Login(r *ghttp.Request)  {
	var (
		data *model.CardLoginParam
		err error
		id int
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	id = r.GetInt("app_id")
	login, err := service.SignCard.Login(r, id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg, login)
}

func (a *apiSignCard) Logout(r *ghttp.Request) {
	var (
		data *model.CardLogoutParam
		err error
		id int
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	id = r.GetInt("app_id")
	err = service.SignCard.Logout(id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg)
}

func (a *apiSignCard) Heart(r *ghttp.Request) {
	var (
		data *model.CardHeartParam
		err error
		id int
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	id = r.GetInt("app_id")
	err = service.SignCard.Heart(id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg)
}

func (a *apiSignCard) Unbind(r *ghttp.Request) {
	var (
		data *model.CardUnbindParam
		err error
		id int
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	id = r.GetInt("app_id")
	unbind, err := service.SignCard.Unbind(id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg, unbind)
}

func (a *apiSignCard) GetCardData(r *ghttp.Request) {
	var (
		data *model.GetCardDataParam
		err error
		id int
		cardData interface{}
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	id = r.GetInt("app_id")
	cardData, err = service.SignCard.GetCardData(id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.JsonExit(r, code.Success, code.SuccessMsg, cardData)
}

func (a *apiSignCard) SetCardData(r *ghttp.Request) {
	var (
		data *model.SetCardDataParam
		err error
		id int
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	if err = service.SignCard.SetCardData(id, data); err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg)
}

func (a *apiSignCard) GetCardInfo(r *ghttp.Request) {
	var (
		data *model.GetCardInfoParam
		err error
		id int
		info interface{}
	)
	if err = r.Parse(&data); err != nil {
		response.JsonExit(r, code.ParamError, err.Error())
	}
	info, err = service.SignCard.GetInfo(id, data)
	if err != nil {
		response.JsonExit(r, code.ServerError, err.Error())
	}
	response.Json(r, code.Success, code.SuccessMsg, info)
}